import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('views/login/Login.vue')
  },
  {
    path: '/home',
    name: 'Home',
    redirect: '/users',
    component: () => import('views/home/Home.vue'),
    children: [
      {
        path: '/users',
        component: () => import('views/home/childrenCpns/main/HomeMainUser.vue')
      },
      {
        path: '/rights',
        component: () => import('views/home/childrenCpns/main/power/PowerRights.vue')
      },
      {
        path: '/roles',
        component: () => import('views/home/childrenCpns/main/power/PowerRoles.vue')
      },
      {
        path: '/categories',
        component: () => import('views/home/childrenCpns/main/goods/GoodsCate.vue')
      },
      {
        path: '/params',
        component: () => import('views/home/childrenCpns/main/goods/GoodsParams.vue')
      },
      {
        path: '/goods',
        component: () => import('views/home/childrenCpns/main/goods/GoodsList.vue')
      },
      {
        path: '/goods/add',
        component: () => import('views/home/childrenCpns/main/goods/GoodsAdd.vue')
      },
      {
        path: '/orders',
        component: () => import('views/home/childrenCpns/main/manage/ManageOrderList.vue')
      },
      {
        path: '/reports',
        component: () => import('views/home/childrenCpns/main/statistics/StatisticsReports.vue')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, form, next) => {
  if (to.path === '/login') return next()
  const token = window.sessionStorage.getItem('token')
  if (!token) {
    return next('/login')
  }
  next()
})

export default router
